package com.qianfeng.goods.service;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import com.qianfeng.goods.bean.Cart;
import com.qianfeng.goods.bean.Order;
import com.qianfeng.goods.bean.OrderGoodsMapping;
import com.qianfeng.goods.mapper.CartMapper;
import com.qianfeng.goods.mapper.OrderGoodsMappingMapper;
import com.qianfeng.goods.mapper.OrderMapper;
import com.qianfeng.utils.DBUtils;

public class OrderService {

	public long saveOrder(Order order) {
		CartMapper cartMapper = DBUtils.getMapper(CartMapper.class);
		OrderMapper orderMapper = DBUtils.getMapper(OrderMapper.class);
		OrderGoodsMappingMapper orderGoodsMappingMapper = DBUtils.getMapper(OrderGoodsMappingMapper.class);
		// 客户端点击去支付时, 会将购物车记录的id和收货地址id传递过来, 我们根据这些购物车id 查出对应的商品
		long[] cartIds = order.getCartIds();
		Cart cart = new Cart();
		cart.setIds(cartIds);
		cart.setUser_id(order.getUser_id());
		// 根据购物车记录的id查询出对应的商品
		List<Cart> carts = cartMapper.getGoodsByCartId(cart);

		// 计算订单中的商品总价格
		double sum = 0;

		for (Cart cart2 : carts) {
			sum += cart2.getPrice() * cart2.getNum();
		}

		order.setPrice(sum);
		order.setCreate_time(System.currentTimeMillis());
		// 保存订单, 并获取订单的id
		orderMapper.saveOrder(order);

		// 保存订单和商品的映射关系
		List<OrderGoodsMapping> list = new ArrayList<OrderGoodsMapping>();
		for (Cart cart2 : carts) {
			OrderGoodsMapping goodsMapping = new OrderGoodsMapping();
			goodsMapping.setOrder_id(order.getId());
			goodsMapping.setGoods_id(cart2.getGoods_id());
			goodsMapping.setPrice(cart2.getPrice());
			goodsMapping.setNum(cart2.getNum());
			goodsMapping.setInco(cart2.getInco());
			goodsMapping.setG_name(cart2.getG_name());

			list.add(goodsMapping);
		}
		for (OrderGoodsMapping orderGoodsMapping : list) {
			orderGoodsMappingMapper.saveMapping(orderGoodsMapping);
		}

		cartMapper.deleteByIds(cart);
		DBUtils.release();
		return order.getId();
	}

	/**
	 * 更新订单支付状态
	 * 
	 * @param order
	 * @return
	 */
	public boolean updateOrderStatus(Order order) {
		OrderMapper orderMapper = DBUtils.getMapper(OrderMapper.class);
		int num = orderMapper.updateOrderStatus(order);
		DBUtils.release();
		return num > 0;
	}

	public List<Order> getList(Order order) {
		OrderMapper orderDao = DBUtils.getMapper(OrderMapper.class);
		List<Order> list = orderDao.getList(order);

		OrderGoodsMappingMapper mappingDao = DBUtils.getMapper(OrderGoodsMappingMapper.class);

		List<List<OrderGoodsMapping>> mappings = new ArrayList<List<OrderGoodsMapping>>();

		for (int i = 0; i < list.size(); i++) {
			Order order2 = list.get(i);

			List<OrderGoodsMapping> li = mappingDao.getListByOrderIds(order2.getId());
			mappings.add(li);
		}

		TreeMap<Long, Order> map = new TreeMap<Long, Order>();

		for (Order order2 : list) {
			map.put(order2.getId(), order2);
		}

		for (List<OrderGoodsMapping> orderGoodsMapping : mappings) {
			for (OrderGoodsMapping o : orderGoodsMapping) {
				long orderId = o.getOrder_id();

				Order order2 = map.get(orderId);
				order2.getMappings().add(o);
			}

		}
		DBUtils.release();
		return list;

	}

	/*
	 * public List<Order> getList(Order order) {
	 * 
	 * OrderMapper orderMapper = DBUtils.getMapper(OrderMapper.class);
	 * OrderGoodsMappingMapper orderGoodsMappingMapper =
	 * DBUtils.getMapper(OrderGoodsMappingMapper.class);
	 * 
	 * List<Order> list = orderMapper.getList(order);
	 * 
	 * long[] orderIds = new long[list.size()];
	 * 
	 * for (int i = 0; i < list.size(); i++) { Order order2 = list.get(i);
	 * orderIds[i] = order2.getId(); }
	 * 
	 * List<OrderGoodsMapping> mappings =
	 * orderGoodsMappingMapper.getListByOrderIds(orderIds);
	 */
	/*
	 * for (OrderGoodsMapping orderGoodsMapping : mappings) { long orderId =
	 * orderGoodsMapping.getGoods_id();
	 * 
	 * for (Order order2 : list) { if(order2.getId()==orderId) {
	 * order2.getMappings().add(orderGoodsMapping); break; } } }
	 */

	/*
	 * HashMap<Long, Order> map = new HashMap<Long, Order>();
	 * 
	 * for (Order order2 : list) { map.put(order2.getId(), order2); }
	 * 
	 * for (OrderGoodsMapping orderGoodsMapping : mappings) { long orderId =
	 * orderGoodsMapping.getOrder_id();
	 * 
	 * Order order2 = map.get(orderId); order2.getMappings().add(orderGoodsMapping);
	 * }
	 * 
	 * return list; }
	 */

	/**
	 * 获取总数
	 * 
	 * @param order
	 * @return
	 */
	public int getCount(Order order) {
		OrderMapper orderMapper = DBUtils.getMapper(OrderMapper.class);
		int num = orderMapper.getCount(order);
		DBUtils.release();
		return num;
	}

}
